define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bill/index' + location.search,
                    multi_url: 'bill/multi',
                    import_url: 'bill/import',
                    table: 'bill',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), operate: false},
                        {field: 'buyerName', title: __('Buyername'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'),formatter: Table.api.formatter.normal,searchList: {0: '个人', 1: '企业'}},
                        {field: 'bill_type', title: '发票类型',formatter: Table.api.formatter.normal,searchList: {1: '蓝票', 2: '红票'}},
                        {field: 'buyerTaxNum', title: __('Buyertaxnum'), operate: 'LIKE'},
                        {field: 'bill_content', title: __('Bill_content'), operate: 'LIKE'},
                        {field: 'bill_money', title: __('Bill_money'), operate:'BETWEEN'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'openBlank', title: __('Openblank'), operate: 'LIKE'},
                        {field: 'buyerAccount', title: __('Buyeraccount'), operate: 'LIKE'},
                        {field: 'buyerAddress', title: __('Buyeraddress'), operate: 'LIKE'},
                        {field: 'buyerTel', title: __('Buyertel'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),formatter: Table.api.formatter.normal,searchList: {1: '待提交', 2: '开票完成',20:'开票中',21:'开票成功签章中',22:'开票失败',24:'开票成功签章失败',3:'发票已作废',31:'发票作废中'}},
                        {field: 'client_status', title: '客户端状态',formatter: Table.api.formatter.normal,searchList: {0: '开票中', 1: '开票成功',2:'发票作废',3:'开票失败',4:'作废中'}},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE'},

                        {field: 'member_id', title: __('Member_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false},

                        {field: 'orderNo', title: __('Orderno'), operate: 'LIKE'},
                        {field: 'pdfUrl', title: __('Pdfurl'), operate: false, formatter: Table.api.formatter.url},
                        {field: 'invoiceSerialNum', title: __('Invoiceserialnum'), operate: 'LIKE'},
                        {field: 'invoiceDate', title: __('Invoicedate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'orderId', title: __('Orderid'),operate: false,sortable: true,},
                        {field: 'invoiceNo', title: __('Invoiceno'), operate: 'LIKE'},
                        {field: 'cancelInvoiceId', title: __('Cancelinvoiceid'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量重新提交
            $(document).on("click", ".btn-againt", function () {
                var ids = Table.api.selectedids(table);
                Layer.open({
                    title: "重新提交",
                    content: '确认重新提交选中的'+ids.length+'个发票吗?',
                    btn: ['确定','取消'],
                    yes: function (index, layero) {
                        Fast.api.ajax({
                            url: "bill/againt",
                            type: "post",
                            data: {ids: ids.join(',')},
                        }, function () {
                            table.bootstrapTable('refresh', {});
                            Layer.close(index);
                        });
                    },
                    success: function (layero, index) {
                    }
                });
            });
            //批量设为失败
            $(document).on("click", ".btn-fail", function () {
                var ids = Table.api.selectedids(table);
                Layer.open({
                    title: "开票失败",
                    content: '确认设置选中的'+ids.length+'个发票为开票失败吗?',
                    btn: ['确定','取消'],
                    yes: function (index, layero) {
                        Fast.api.ajax({
                            url: "bill/fail",
                            type: "post",
                            data: {ids: ids.join(',')},
                        }, function () {
                            table.bootstrapTable('refresh', {});
                            Layer.close(index);
                        });
                    },
                    success: function (layero, index) {
                    }
                });
            });







        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
